package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.MobileMessage;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.basic.MobileMessageService;
import cc.yugu.yhdw.webserver.service.basic.SmsConfigService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
/**
 * 短信发送记录控制器（发送如充值记录短信到客户手机）
 * 
 * @Date:Created in 2018/7/23 14:57
 * @Author:
 * @Modified By: zjn 2018/7/23 14:57
 */ 
@Controller
@RequestMapping(value = "/security/basic/mobile_message")
public class MobileMessageController extends SecurityController {

    @Autowired
    MobileMessageService mobileMessageService;
    @Autowired
    SmsConfigService smsConfigService;

    @RequestMapping(value = "index.htm")
    public void index(Model model,HttpServletRequest request) {
        model.addAttribute("messageStatusEnum", MobileMessage.MessageStatus.values());
        model.addAttribute("smsConfigList", smsConfigService.findByAgent(getSessionUser(request).getAgentId()));
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_05_02.getValue());
    }
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.json.PageResult
    * @Author By: zjn 2018/7/23  15:03
    */ 
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(MobileMessage search) {
        return PageResult.successResult(mobileMessageService.findPage(search));
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "view.htm")
    public String view(Model model, long id) {
        model.addAttribute("messageStatusEnum", MobileMessage.MessageStatus.values());

        MobileMessage entity = mobileMessageService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }

        model.addAttribute("entity", entity);
        return "/security/basic/mobile_message/view";
    }
    /**
    * 短信发送成功 or 失败
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/23  15:09
    */ 
    @RequestMapping("send.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult send(long id) {
        int effect = mobileMessageService.updateStatus(id, MobileMessage.MessageStatus.NOT.getValue());
        if(effect > 0) {
            return ExtResult.successResult();
        } else {
            return ExtResult.failResult("记录不存在");
        }
    }


}
